package org.jeecg.modules.qrCode.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.WriterException;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.MinioUtil;
import org.jeecg.modules.qrCode.service.QrCodeService;
import org.jeecg.modules.system.entity.SysFiles;
import org.jeecg.modules.system.service.ISysFilesService;
import org.jeecg.modules.utils.QRCodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * @description: QR CODE SERVICE IMPL
 * @author: zwf
 * @create: 2025-06-23 15:57
 **/
@Service
public class QrCodeServiceImpl implements QrCodeService {

    @Resource
    private ISysFilesService filesService;

    /**
     * 创建二维码
     *
     * @author zwf
     * @date created in 2025-06-23 15:58
     * @param: params
     * @return: org.jeecg.common.api.vo.Result<java.lang.String>
     */
    @Override
    public Result<String> create(JSONObject params) {
        String value = params.getString("value");
        String logo = params.getString("logo");
        if (logo == null || logo.isEmpty()) {
            logo = "https://img.zwfcyy.top/echo-app/logo/er-main.png";
        }
        try {
            Snowflake snowflake = IdUtil.getSnowflake();
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

            InputStream stream;
            if (value.startsWith("http://") || value.startsWith("https://")) {
                stream = QRCodeUtil.generateQRUrlStream(value, 1000, 1000);
            } else {
                stream = QRCodeUtil.generateQRCodeStream(value, 1000, 1000);
            }

            String fileName = "QR-" + snowflake.nextId() + ".png";

            String fileUrl = MinioUtil.upload(stream, "qr/" + fileName);

            if (fileUrl != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                byte[] buffer = new byte[1024];

                int length;

                while ((length = stream.read(buffer)) != -1) {
                    baos.write(buffer, 0, length);
                }
                byte[] bytes = baos.toByteArray();

                int fileSize = bytes.length;

                SysFiles file = new SysFiles();

                file.setFileName(fileName);
                file.setUrl(fileUrl);
                file.setFileType("image");
                file.setStoreType("app-move");
                file.setFileSize(fileSize / 1024.0);
                file.setDelFlag("0");
                file.setCreateTime(new Date());
                file.setCreateBy(sysUser.getId());

                filesService.save(file);

                baos.close();
                stream.close();

                return Result.ok(fileUrl);
            } else {
                return Result.error("二维码保存失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("二维码生成失败");
        }

    }
}
